/*
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2025 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.axelor.apps.account.service.invoice.workflow.ventilate;

import com.axelor.apps.account.db.repo.InvoiceRepository;
import com.axelor.apps.account.service.accountingsituation.AccountingSituationService;
import com.axelor.apps.account.service.app.AppAccountService;
import com.axelor.apps.account.service.config.AccountConfigService;
import com.axelor.apps.account.service.fixedasset.FixedAssetGenerationService;
import com.axelor.apps.account.service.invoice.AdvancePaymentRefundService;
import com.axelor.apps.account.service.invoice.InvoiceJournalService;
import com.axelor.apps.account.service.invoice.InvoicePfpValidateService;
import com.axelor.apps.account.service.invoice.InvoiceTermPfpToolService;
import com.axelor.apps.account.service.invoice.InvoiceTermService;
import com.axelor.apps.account.service.move.MoveCreateFromInvoiceService;
import com.axelor.apps.base.AxelorException;
import com.axelor.apps.base.service.administration.SequenceService;
import com.axelor.apps.base.service.user.UserService;
import com.google.common.base.Preconditions;
import com.google.inject.Inject;
import com.google.inject.servlet.RequestScoped;

@RequestScoped
public class VentilateAdvancePaymentRefundState extends VentilateState {

  protected AdvancePaymentRefundService advancePaymentRefundService;

  @Inject
  public VentilateAdvancePaymentRefundState(
      SequenceService sequenceService,
      MoveCreateFromInvoiceService moveCreateFromInvoiceService,
      AccountConfigService accountConfigService,
      AppAccountService appAccountService,
      InvoiceRepository invoiceRepo,
      WorkflowVentilationService workflowService,
      UserService userService,
      FixedAssetGenerationService fixedAssetGenerationService,
      InvoiceTermService invoiceTermService,
      AccountingSituationService accountingSituationService,
      InvoiceJournalService invoiceJournalService,
      InvoicePfpValidateService invoicePfpValidateService,
      InvoiceTermPfpToolService invoiceTermPfpToolService,
      AdvancePaymentRefundService advancePaymentRefundService) {
    super(
        sequenceService,
        moveCreateFromInvoiceService,
        accountConfigService,
        appAccountService,
        invoiceRepo,
        workflowService,
        userService,
        fixedAssetGenerationService,
        invoiceTermService,
        accountingSituationService,
        invoiceJournalService,
        invoicePfpValidateService,
        invoiceTermPfpToolService);
    this.advancePaymentRefundService = advancePaymentRefundService;
  }

  @Override
  public void process() throws AxelorException {

    Preconditions.checkNotNull(invoice.getPartner());

    setDate();
    setJournal();
    setPartnerAccount();
    setInvoiceId();
    updatePaymentSchedule();
    updateAdvancePaymentAmounts();
    // we don't create the move
    // and the invoice stays validated

    workflowService.afterVentilation(invoice);
  }

  protected void updateAdvancePaymentAmounts() throws AxelorException {
    advancePaymentRefundService.updateAdvancePaymentAmounts(invoice);
  }
}
